package com.ecoolex.ying.space.admin.graphql;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.Admin;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.EstateProjectActivityCoupon;
import com.ecoolex.ying.space.entity.EstateProjectActivityCoupon.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityCouponOrder;
import com.ecoolex.ying.space.entity.EstateProjectActivityCouponPackage;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityCouponDTO;
import com.ecoolex.ying.space.pojo.EditEstateProjectActivityCouponDTO;
import com.ecoolex.ying.space.ws.EstateProjectActivityCouponOrderService;
import com.ecoolex.ying.space.ws.EstateProjectActivityService;
import com.google.common.collect.Lists;

@GraphQL2Module(type = Admin, description = "地产项目活动代金券管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = EstateProjectActivityCouponGraphql.AUTH, name = "地产项目活动代金券管理")
public class EstateProjectActivityCouponGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-ESTATE-PROJECT-ACTIVITY-COUPON";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	private static final String AUTH_PACKAGE_GET = AUTH + "-PACKAGE-GET";
	
	private static final String AUTH_PACKAGE_QUERY = AUTH + "-PACKAGE-QUERY";
	
	private static final String AUTH_ONLINE = AUTH + "-ONLINE";
	
	private static final String AUTH_OFFLINE = AUTH + "-OFFLINE";
	
	@Reference
	private EstateProjectActivityService estateProjectActivityService;
	
	@Reference
	private EstateProjectActivityCouponOrderService couponOrderService;
	
	@Autowired
    private ConsumerMemberManager consumerMemberManager;
	
	@TypeField(type = EstateProjectActivityCouponPackage.class,field = "couponOrder",description = "优惠券订单")
	public EstateProjectActivityCouponOrder couponOrder(@Source EstateProjectActivityCouponPackage source) {
		return couponOrderService.getByOrderId(source.getOrderId());
	}
	
	@TypeField(type = EstateProjectActivityCouponPackage.class,field = "coupon",description = "发行的优惠券")
	public EstateProjectActivityCoupon coupon(@Source EstateProjectActivityCouponPackage source) {
		return estateProjectActivityService.getEstateProjectActivityCoupon(source.getCouponId());
	}
	
	@TypeField(type = EstateProjectActivityCoupon.class,field = "canDelete",description = "能删除")
	public boolean canDelete(@Source EstateProjectActivityCoupon source, AdminMemberAuthority user) {
		return user.auth(AUTH_DEL);
	}
	
	@TypeField(type = EstateProjectActivityCoupon.class,field = "canOnline",description = "能上线")
	public boolean canOnline(@Source EstateProjectActivityCoupon source, AdminMemberAuthority user) {
		return !source.getOnline() && user.auth(AUTH_ONLINE);
	}
	
	@TypeField(type = EstateProjectActivityCoupon.class,field = "canOffline",description = "能下线")
	public boolean canOffline(@Source EstateProjectActivityCoupon source, AdminMemberAuthority user) {
		return source.getOnline() && user.auth(AUTH_OFFLINE);
	}
	
	@TypeField(type = EstateProjectActivityCouponPackage.class,field = "userInfo",description = "用户信息")
	public ConsumerMember user(@Source EstateProjectActivityCouponPackage source) {
		return consumerMemberManager.getByUserId(source.getUserId());
	}
	
	@QueryField(field = "estateProjectActivityCouponPackage" , description = "用户购买代金券详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_PACKAGE_GET, name = "用户购买代金券详情")
	public EstateProjectActivityCouponPackage getCouponPackage(@NonNull Integer id,AdminMemberAuthority user) {
		user.assertAuth(AUTH_PACKAGE_GET);
		return estateProjectActivityService.getCouponPackage(id);
	}
	
	@QueryField(field = "estateProjectActivityCouponPackageList" , description = "用户购买代金券包列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_PACKAGE_QUERY, name = "用户购买代金券列表")
	public Pagination<EstateProjectActivityCouponPackage> queryCouponPackage(String code,Integer status,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user){
		user.assertAuth(AUTH_PACKAGE_QUERY);
		return estateProjectActivityService.queryCouponPackage(code, status, sorts, page);
	}
	
	@QueryField(field = "estateProjectActivityCoupon" , description = "地产项目活动代金券详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "地产项目活动代金券详情")
	public EstateProjectActivityCoupon get(@NonNull Integer couponId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return estateProjectActivityService.getEstateProjectActivityCoupon(couponId);
	}
	
	@QueryField(field = "estateProjectActivityCouponList" , description = "地产项目活动代金券列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "地产项目活动代金券列表")
	public Pagination<EstateProjectActivityCoupon> query(@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user) {
		user.assertAuth(AUTH_QUERY);
		return estateProjectActivityService.queryEstateProjectActivityCoupon(sorts, page);
	}
	
	@MutationField(field = "addEstateProjectActivityCoupon", description = "添加地产项目活动代金券")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加地产项目活动代金券")
	public EstateProjectActivityCoupon add(@NonNull AddEstateProjectActivityCouponDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return estateProjectActivityService.addEstateProjectActivityCoupon(record, user.userName());
	}
	
	@MutationField(field = "editEstateProjectActivityCoupon", description = "编辑地产项目活动代金券")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑地产项目活动代金券")
	public EstateProjectActivityCoupon edit(@NonNull EditEstateProjectActivityCouponDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		EstateProjectActivityCoupon old = estateProjectActivityService.getEstateProjectActivityCoupon(record.getCouponId());
		BizAssert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目活动代金券不存在");
		BeanUtil.copy(record,
            old,
            Lists.newArrayList(
            		Column.online,
                    Column.deleted,
                    Column.createUser,
                    Column.createTime).stream().map(Column::getJavaProperty).toArray(String[]::new));
		return estateProjectActivityService.editEstateProjectActivityCoupon(old, user.userName());
	}
	
	@MutationField(field = "delEstateProjectActivityCoupon", description = "删除地产项目活动代金券")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除地产项目活动代金券")
	public boolean delete(@NonNull Integer couponId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		EstateProjectActivityCoupon old = estateProjectActivityService.getEstateProjectActivityCoupon(couponId);
		BizAssert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目活动代金券不存在");
		estateProjectActivityService.delEstateProjectActivityCoupon(old, user.userName());
		return true;
	}
	
	@MutationField(field = "onlineEstateProjectActivityCoupon", description = "上线地产项目活动代金券")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ONLINE, name = "上线地产项目活动代金券")
	public boolean online(@NonNull Integer couponId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ONLINE);
		EstateProjectActivityCoupon old = estateProjectActivityService.getEstateProjectActivityCoupon(couponId);
		BizAssert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目活动代金券不存在");
		estateProjectActivityService.onlineEstateProjectActivityCoupon(old, user.userName());
		return true;
	}
	
	@MutationField(field = "offlineEstateProjectActivityCoupon", description = "下线地产项目活动代金券")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OFFLINE, name = "下线地产项目活动代金券")
	public boolean offline(@NonNull Integer couponId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_OFFLINE);
		EstateProjectActivityCoupon old = estateProjectActivityService.getEstateProjectActivityCoupon(couponId);
		BizAssert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目活动代金券不存在");
		estateProjectActivityService.offlineEstateProjectActivityCoupon(old, user.userName());
		return true;
	}
}
